# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        Young  
# Name:         demo7_08
# Description:
# Author:       asus
# Date:         2019/3/14
#-------------------------------------------------------------------------------


#定义addNumbers函数，该函数指定了一个可变参数numbers
def addNumbers(*numbers):
    result = 0
    for number in numbers:
        result += number
    return result

# 调用addNumbers函数，运行结果：15
print(addNumbers(1,2,3,4,5))
print("----------------")

# 定义calculator函数，在可变参数numbers前面加一个普通形参type
def calculator(type,*numbers):
    result = 0
    # type参数的值可以为add,sub,mul,div

    # 进行加法
    if type == 'add':
        for number in numbers:
            result += number
    # 进行减法
    elif type == 'sub':
        result = numbers[0]
        for i in range(1,len(numbers)):
            result -= numbers[i]
    # 进行乘法
    elif type == 'mul':
        result = 1
        for number in numbers:
            result *= number
    # 进行除法
    else:
        result = numbers[0]
        for i in range(1,len(numbers)):
            result /= numbers[i]
    return result

# 调用calculator函数，会将第一个实参作为type参数的值传给calculator函数

print(calculator('add',1,2,3,4,5,6))
print(calculator('sub',1234,44,54,12,57))
print(calculator('mul',1,2,3,4,5,6,7))
print(calculator('div',100,2,5))
print("---------------")


# 定义calculator函数，在可变参数numbers后面指定一个普通的形参ratio
def calculator1(type,*numbers,ratio):
    return calculator(type,*numbers) * ratio

print(calculator1('add',1,2,3,4,5,6,ratio=3))
print(calculator1('sub',1234,44,54,12,57,ratio = 2))
print(calculator1('mul',1,2,3,4,5,6,7,ratio = 4))
print(calculator1('div',100,2,5,ratio = 4))
print("----------------")

# 定义calcultor2函数，为ratio参数指定一个默认值
def calculator2(type,*numbers,ratio = 4):
    return calculator(type,*numbers) * ratio

print(calculator2('add',1,2,3,4,5,6))
